package com.xhb.campusservices.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xhb.campusservices.common.lang.Result;
import com.xhb.campusservices.enums.AuditState;
import com.xhb.campusservices.enums.FeedbackState;
import com.xhb.campusservices.pojo.Feedback;
import com.xhb.campusservices.pojo.Task;
import com.xhb.campusservices.pojo.feedbackDto.FeedbackList;
import com.xhb.campusservices.pojo.feedbackDto.ReplyFB;
import com.xhb.campusservices.pojo.orderDto.OrderAdmin;
import com.xhb.campusservices.pojo.teskDto.TaskAdmin;
import com.xhb.campusservices.service.*;
import com.xhb.campusservices.util.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 任务管理
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminTOService admin;

    @Autowired
    TaskService taskService;

    @Autowired
    FeedbackService feedbackService;

    @Autowired
    FeedbackListService feedbackListService;

    @Autowired
    JWTUtils jwtUtils;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 管理员任务管理
     * @return
     */
    @GetMapping("/tAdmin")
    public Result tAdmin(){
        List<TaskAdmin> taskAdmins = admin.tAdmin();
        return Result.succ(taskAdmins);
    }

    /**
     * 管理员订单管理
     * @return
     */
    @GetMapping("/oAdmin")
    public Result oAdmin(){
        List<OrderAdmin> orderAdmins = admin.oAdmin();
        return Result.succ(orderAdmins);
    }

    /**
     * 查看未审核的任务
     *
     * @return
     */
    @GetMapping("/notAudit")
    public Result notAudit() {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getTaskstate, 0)
                .eq(Task::getAuditstate, 1)
                .orderByDesc(Task::getReleasetime);
        List<Task> list = taskService.list(queryWrapper);
        if (list.size() == 0) {
            return Result.succ(200, "没有未审核的任务~", null);
        }
        return Result.succ(list);
    }

    /**
     * 修改审核未通过的订单
     *
     * @param id
     * @return
     */
    @GetMapping("/updateAudit/{id}")
    public Result updateAudit(@PathVariable(name = "id") Long id) {
        LambdaUpdateWrapper<Task> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Task::getId, id)
                .set(Task::getAuditstate, AuditState.已通过)
                .set(Task::getReleasetime,sdf.format(new Date()));
        if (taskService.update(updateWrapper)) {
            return Result.succ(200, "审核通过", null);
        }
        return Result.fail("审核未通过");
    }

    /**
     * 管理员查看用户反馈信息表
     * @return
     */
    @GetMapping("/fbAdmin")
    public Result fbAdmin(){
        List<FeedbackList> fbLists = feedbackListService.feedbackListAdmin();
        return  Result.succ(fbLists);
    }

    /**
     * 回复用户反馈信息
     * @param replyFB
     * @return
     */
    @PostMapping("/reply")
    public Result reply(@Validated @RequestBody ReplyFB replyFB){
        LambdaUpdateWrapper<Feedback> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Feedback::getId,replyFB.getId())
                .set(Feedback::getReplycontent,replyFB.getReplycontent())
                .set(Feedback::getState, FeedbackState.已回复)
                .set(Feedback::getReplytime,sdf.format(new Date()));
        if (feedbackService.update(updateWrapper)) {
            return Result.succ(200, "回复成功", null);
        }
        return Result.fail("回复失败");
    }
}
